package com.dataseek.iot.sys.manager.handler;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dataseek.iot.core.constant.ResponseConst;
import com.dataseek.iot.core.handler.ServiceHandler;
import com.dataseek.iot.core.response.ResponseEntity;
import com.dataseek.iot.mysql.entity.Area;
import com.dataseek.iot.mysql.entity.Area;
import com.dataseek.iot.mysql.entity.City;
import com.dataseek.iot.mysql.entity.Line;
import com.dataseek.iot.mysql.service.impl.AreaServiceImpl;
import com.dataseek.iot.mysql.service.impl.LineServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;

/**
 * @author cc
 * @Title: 区域服务handler
 * @Description: 区域服务handler
 * @date 2021/10/267:03 下午
 * @since 1.0
 */
@Service
@Slf4j
public class AreaServiceHandler extends ServiceHandler<Area> {

    @Autowired
    AreaServiceImpl areaService;

    @Autowired
    LineServiceImpl lineService;



    @Override
    public Mono<ResponseEntity> saveOne(Object entity) {
        Boolean b = areaService.save((Area) entity);
        return this.createResponse(b);
    }

    @Override
    public Mono<ResponseEntity> saveBatch(List lists) {
        Boolean b = areaService.saveBatch(lists);
        return this.createResponse(b);
    }


    @Override
    public Mono<ResponseEntity> removeById(String id) {
        ResponseEntity entity = new ResponseEntity();
        QueryWrapper queryWrapper  = new QueryWrapper();
        queryWrapper.eq("area_code", id);
        List list = lineService.list(queryWrapper);
        if(list.size()>0) {
            entity.setCode(ResponseConst.CRUD_FAILED_CODE);
            entity.setMsg("区域有关联的线路信息，请先删除或修改相关线路");
            entity.setData(list);
            return Mono.just(entity);
        }else {
            queryWrapper = new QueryWrapper();
            queryWrapper.eq("area_id", id);
            Boolean b = areaService.removeById(id);
            return this.createResponse(b);
        }
    }

    @Override
    public Mono<ResponseEntity> removeAll(List ids) {
        ResponseEntity entity = new ResponseEntity();
        QueryWrapper queryWrapper  = new QueryWrapper();
        queryWrapper.in("area_code", ids);
        List list = lineService.list(queryWrapper);
        if(list.size()>0){
            entity.setCode(ResponseConst.CRUD_FAILED_CODE);
            entity.setMsg("区域有关联的线路信息，请先删除或修改相关线路");
            entity.setData(list);
            return Mono.just(entity);
        }else{
            queryWrapper = new QueryWrapper();
            queryWrapper.in("area_id", ids);
            Boolean b = areaService.remove(queryWrapper);
            return this.createResponse(b);
        }
    }

    @Override
    public Mono<ResponseEntity> updateOne(Object entity) {
        Boolean b = areaService.updateById((Area) entity);
        return this.createResponse(b);
    }

    @Override
    public Mono<ResponseEntity> updateBatch(List entities) {
        Boolean b =  areaService.updateBatchById(entities);
        return this.createResponse(b);
    }

    @Override
    public Mono<ResponseEntity> findOne(String id) {
        Area auth = areaService.getById(id);
        return this.createResponse(auth);
    }

    public Mono<ResponseEntity> findOneByCode(String code){
        Area area = areaService.getOneByCode(code);
        return this.createResponse(area);
    }

    @Override
    public Mono<ResponseEntity> page(Page p) {
        Page authPage = areaService.page(p);
        return this.createResponse(authPage);
    }

    @Override
    public Mono<ResponseEntity> page(Page p, Wrapper wrapper) {
        Page authPage = areaService.page(p, wrapper);
        return this.createResponse(authPage);
    }

    @Override
    public Flux<ResponseEntity> find(List ids) {
        List<Area> lists = areaService.listByIds(ids);
        return this.createResponse(lists);
    }

    @Override
    public Flux<ResponseEntity> find(Wrapper wrapper) {
        List<Area> lists = areaService.list(wrapper);
        return this.createResponse(lists);
    }

    @Override
    public Flux findAll() {
        List<Area> lists = areaService.list();
        return this.createResponse(lists);
    }

    @Override
    public Mono<ResponseEntity> remove(Wrapper wrapper) {
        List<Area> areas = areaService.list(wrapper);
        ResponseEntity entity = new ResponseEntity();
        for(Area a : areas){
            List<Line> lines = lineService.getLinesByAreaCode(a.getAreaId());
            if(lines.size()>0){
                log.error("待删除区县信息中有线路关联，请先修改相关线路");
                entity.setMsg("待删除区县信息中有线路关联，请先修改相关线路");
                entity.setCode(ResponseConst.CRUD_FAILED_CODE);
                entity.setData(lines);
                return Mono.just(entity);
            }
        }
        Boolean b = areaService.remove(wrapper);
        return this.createResponse(b);
    }

    @Override
    public Mono<ResponseEntity> remove(String id) {
        Boolean b = areaService.removeById(id);
        return this.createResponse(b);
    }

    @Override
    public Mono<ResponseEntity> update(Wrapper wrapper) {
        Boolean b = areaService.update(wrapper);
        return this.createResponse(b);
    }

    @Override
    public Mono<ResponseEntity> defaultResponseOne(ResponseEntity responseEntity) {
        return Mono.just(responseEntity);
    }

    @Override
    public Flux<ResponseEntity> defaultResponseTwo(ResponseEntity responseEntity) {
        return Flux.just(responseEntity);
    }
}
